<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript">
	/* Handles both keyup and mouse click event on a select box */
	$(function() {
		$('#eppdNameSelect').bind('change keyup',function () {
			var serverName = $(this).attr('value');
			if(serverName === "") {
				setDataToNull();
				return;
			} 
			getServerInfo();
		}).change();
	});
	
	function setDataToNull() {
		$("#eppdFriendlyname").val('');
		$("#ipAddress").val('');
		$("#portNumber").val('');
		$("#username").val('');
		$("#password").val('');
		$("#pcksFile").val('');
		$("#jksFile").val('');
		$('input[type=checkbox][name=editExtensionss]').each(function () {
			$('#' + $(this).id()).prop('checked', false);
		});
	}

	function setData(data) {
		var eppd_server = data.eppd_server;
		$("#eppdFriendlyname").val(eppd_server.friendlyName);
		$("#ipAddress").val(eppd_server.eppdIp);
		$("#portNumber").val(eppd_server.eppdPort);
		$("#username").val(eppd_server.eppdUsername);
		$("#password").val(eppd_server.eppdPassword);
		$("#pcksFile").val(eppd_server.pcksFileName);
		$("#jksFile").val(eppd_server.jksFileName);
		
		var extensions = eppd_server.extensions;
		var extensions_arr = extensions.split(',');
		
		$('input[type=checkbox][name=editExtensions]').each(function () {
		    var current_checkbox_id = $(this).attr('id');
    		$('#' + current_checkbox_id).prop('checked', false);
		});
		
		$('input[type=checkbox][name=editExtensions]').each(function () {
		    var current_checkbox_name = $(this).val();
		    var current_checkbox_id = $(this).attr('id');
		    // for each element in the array , if it matches the current element then check it 
		    $.each(extensions_arr, function( index, current_extension) {
		    	if(current_extension.trim().toLowerCase() == current_checkbox_name.trim().toLowerCase()) {
		    		$('#' + current_checkbox_id).prop('checked', true);
		    	}
		    });
		});
	}

	function getServerInfo() {
		var serialized = $('#getServerInfoFrm').serialize();
		var serverName = $("#eppdNameSelect option:selected").val();
		$('#message').html('<img src=\"images/loading.gif\"/> Getting settings for server ' + serverName);
		$.getJSON("/eftweb/get_settings.html", {
			eppdServerName : serverName
		}, function(data) {
			setData(data);
			$('#message').empty();
		});

	}
	
	function updateServerInfo() {
		var serverName = $("#eppdNameSelect option:selected").val();
		var serialized = $('#getServerInfo').serialize();
		$('#message').html('<img src=\"images/loading.gif\"/> Updating settings for server ' + serverName);
		$.post("/eftweb/update_settings.html", serialized, function(data) {
			$('#message').empty();
			$('#message').html('Succesfull made those changes');
		});

	}
</script>
</head>
<body bgcolor="#F7690A">
	<form id="getServerInfoFrm" name="getServerInfoFrm">
		<s:label value="Please select a Daemon"></s:label>
		<select id="eppdNameSelect">
			<option value="">Please select a daemons</option>
			<s:iterator value="eppdServerList">
				<option value="${FriendlyName}">
					<s:property value="FriendlyName" />
				</option>
			</s:iterator>
		</select><br> <br>
	</form>
	<div id="server_settings" class="wrap">
		<s:form id="getServerInfo" method="POST">
				<s:textfield label="EPPD Friendly Name" name="friendlyName"
					id="eppdFriendlyname" maxlength="15"></s:textfield>
				<s:textfield label="EPPD Ip address" name="ipAddress" id="ipAddress"
					maxlength="15"></s:textfield>
				<s:textfield label="EPPD Port Number" name="portNumber"
					id="portNumber" maxlength="4"></s:textfield>
				<s:textfield label="Username" name="username" id="username"
					maxlength="10"></s:textfield>
				<s:textfield label="Password" name="password" id="password"
					maxlength="10" required="true"></s:textfield>
				<s:textfield label="X.509 Certificate PCKS2" name="pcksFile" readonly="true"
					id="pcksFile"></s:textfield>
				<s:textfield label="X.509 Certificate JKS- A" name="jksFile" readonly="true"
					id="jksFile"></s:textfield>
				<s:checkboxlist list="extensionsList" name="editExtensions" label="EPP Extensions" />
				<s:submit align="left" value="Save" onclick="updateServerInfo();return false;"/>
		</s:form>
			<div id="message"></div>
			<div id="status"></div>
	</div>
</body>
</html>
